package mblog.core.persist.dao.impl;

import mblog.core.persist.dao.MaterialDao;
import mblog.core.persist.entity.PMaterialPO;
import mtons.modules.annotation.Repository;
import mtons.modules.persist.impl.BaseRepositoryImpl;
import mtons.modules.pojos.Paging;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import java.util.List;

/**
 * @author Beldon
 */
@Repository(entity = PMaterialPO.class)
public class MaterialDaoImpl extends BaseRepositoryImpl<PMaterialPO> implements MaterialDao {
	private static final long serialVersionUID = 754755214307906383L;

	@Override
	@SuppressWarnings("unchecked")
    public List<PMaterialPO> findAll(){
        Query query = createQuery("from PMaterialPO am order by am.updateTime desc");
        return query.list();
    }

    @Override
    public List<PMaterialPO> paging(Paging paging, String key) {
        PagingQuery<PMaterialPO> q = pagingQuery(paging);
        if (StringUtils.isNotBlank(key)) {
            q.add(Restrictions.or(
                    Restrictions.like("name", key, MatchMode.ANYWHERE),
                    Restrictions.like("model", key, MatchMode.ANYWHERE)
            ));
        }
        q.add(Restrictions.eq("isDel",0));
        q.desc("id");
        return q.list();
    }

    @Override
    public List<PMaterialPO> getByNameAndModel(String name, String model) {
        List<PMaterialPO> list = find(Restrictions.eq("name", name),Restrictions.eq("name", model),Restrictions.eq("isDel",0));
        return list;
    }
}
